package com.qmgyl.marketshop.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.qmfresh.service.shop.facade.dto.ShopSsuQuery;


/**
 * @author donglin
 *
 */
public class HttpUtils {
	
	/**
	 * post请求
	 * @param url
	 * @param params
	 * @return
	 */
	public static String post(String url,Map<String,String> params){
		CloseableHttpClient http = HttpClients.custom().build();
		String html = "";
		try {
			HttpPost post = new HttpPost(url);
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			Set<String> keys = params.keySet();
			for (String key : keys) {
				nvps.add(new BasicNameValuePair(key, params.get(key)));
			}
			post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			
			HttpResponse response = http.execute(post);
			if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
				HttpEntity entity = response.getEntity();
				html = EntityUtils.toString(entity, "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				http.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return html;
	}
	
	/**
	 * 支持requestBody方式 提交 Post请求
	 * @param url
	 * @param obj JSON对象
	 * @return
	 */
	public static String postRequestBody(String url,Object obj){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String token = (String)request.getSession().getAttribute("token");
		CloseableHttpClient http = HttpClients.custom().build();
		String html = "";
		try {
			HttpPost post = new HttpPost(url);
			post.addHeader("auth", token);
			if(obj != null) {
				String req = JSONObject.toJSONString(obj);
				post.setEntity(new ByteArrayEntity(req.getBytes("UTF-8"),ContentType.APPLICATION_JSON));
			}
			HttpResponse response = http.execute(post);
			if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
				HttpEntity entity = response.getEntity();
				html = EntityUtils.toString(entity, "UTF-8");
			}
			System.out.println(html);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				http.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return html;
	}
	public static void main(String[] args) {
		
		ShopSsuQuery shopSsuQuery = new ShopSsuQuery();
		
		String result = postRequestBody("http://localhost:8080/shop/ssu/productSsuList",shopSsuQuery);
		
		//System.out.println(result);
		//DeliveryQuery dq = new DeliveryQuery();
//		dq.setCompanyId(1);
//		dq.setEndDeliveryTime(22);
//		dq.setShopId(3);
//		postRequestBody("http://localhost:8080/qmfresh-service/shop/delivery/list",dq);
	}
}
